<?php

namespace app\admin\controller;
use app\api\model\Privatechat;
use think\Db;

class Addressgoods extends Common {
    /**
     * @return mixed
     */
    public function index() {
        $where['t1.delete_time'] = 0;

        $search_name = input('param.name');
        $search_type = input('param.search');
        $this->assign('name', $search_name);
        $this->assign('search', $search_type);
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $where['t1.' . $search_type] = $search_name;
            } else {
                $where['t1.' . $search_type] = ['like', "%$search_name%"];
            }
        }

        $start = input('start');
        $end   = input('end');
        $this->assign('start', $start);
        $this->assign('end', $end);
        if ($start && $end) {
            $where['t1.create_time'] = ['between', [strtotime($start), strtotime($end)]];
        } elseif ($start && !$end) {
            $where['t1.create_time'] = ['egt', strtotime($start)];
        } elseif (!$start && $end) {
            $where['t1.create_time'] = ['elt', strtotime($end)];
        }

        $state = input('state');
        if ($state || $state === '0') {
            $this->assign('state', $state);
            $where['t1.state'] = $state;
        }
        $status = input('status');
        if ($status || $status === '0') {
            $this->assign('status', $status);
            $where['t1.status'] = $status;
        }
        $day  = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
        $data = Db::name('user_address_goods')
            ->alias('t1')
            ->join('user_claim_address t2', 't1.address_id = t2.id', 'left')
            ->where($where)
            ->field('t1.*, t2.name addname, t2.image_3 addimage')
            ->order('t1.update_time desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function ($item) use ($day) {
                $item['images']   = explode(',', $item['images']);
                $use_time         = $item['use_time'];
                $item['use_time'] = '';
                if ($use_time) {
                    $use_time    = explode('|', $use_time);
                    $use_time[0] = explode(',', $use_time[0]);
                    $daylen      = sizeof($use_time[0]);
                    $use_times   = [];
                    if ($daylen > 1) {
                        $next        = 1;
                        $index       = 0;
                        $use_times[] = [$day[0]];
                        while ($next) {
                            if ($use_time[0][$next - 1] == $use_time[0][$next] - 1) {
                                $use_times[$index][1] = $day[$use_time[0][$next] - 1];
                                if (isset($use_time[0][$next + 1])) {
                                    $next++;
                                } else {
                                    $next = 0;
                                }
                            } else {
                                $index++;
                                $use_times[$index][0] = $day[$use_time[0][$next] - 1];
                                if (isset($use_time[0][$next + 1])) {
                                    $next++;
                                } else {
                                    $next = 0;
                                }
                            }
                        }
                        foreach ($use_times as $key => $value) {
                            $use_times[$key] = implode('至', $use_times[$key]);
                        }
                    } else {
                        foreach ($use_time[0] as $key => $value) {
                            $use_times[] = $day[$value - 1];
                        }
                    }
                    $item['use_time'] = implode(',', $use_times) . ' ' . $use_time[1];
                } else {
                    $item['use_time'] = '店铺营业时间';
                }
                return $item;
            });

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * @param $id
     * @param $shenhe
     * @param $reason
     */
    public function shenhe($id, $shenhe, $reason = '') {
        $time               = time();
        $user_address_goods = Db::name('user_address_goods')->where(['id' => $id])->find();
        $user_claim_address = Db::name('user_claim_address')->where(['id' => $user_address_goods['address_id']])->find();
        $result             = Db::name('user_address_goods')->where(['id' => $id])->update(['status' => $shenhe, 'reason' => $reason, 'update_time' => $time]);
        if ($result) {
            if ($user_claim_address['user_id']) {
                switch ((int) $shenhe) {
                case -1:
                    Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】被驳回" . ($reason ? "，驳回原因：" . $reason : "") . "，请重新编辑后再发布", '');
                    break;
                case 0:
                    Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】重新审核中", '');
                    break;
                case 1:
                    Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】审核通过", '');
                    break;
                }
            }
            return ['ret' => '操作成功'];
        } else {
            return ['err' => '操作失败'];
        }
    }

    /**
     * @return mixed
     */
    public function shenheall() {
        if (request()->isAjax()) {
            $post   = input('post.');
            $shenhe = input('type');
            $reason = '';
            $time   = time();
            if (input('?post.delAll')) {
                $post    = $post['delAll'];
                $userids = [];
                foreach ($post as $k => $id) {
                    $user_address_goods = Db::name('user_address_goods')->where(['id' => $id])->find();
                    $user_claim_address = Db::name('user_claim_address')->where(['id' => $user_address_goods['address_id']])->cache(10)->find();
                    $userids[]          = $user_claim_address['user_id'];
                    if ($shenhe == 'del') {
                    } else {
                        if (!$user_claim_address['user_id']) {
                            continue;
                        }
                        switch ((int) $shenhe) {
                        case -1:
                            Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】被驳回" . ($reason ? "，驳回原因：" . $reason : "") . "，请重新编辑后再发布", '');
                            break;
                        case 0:
                            Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】重新审核中", '');
                            break;
                        case 1:
                            Privatechat::notice($user_claim_address['user_id'], "您发布的商品【" . $user_address_goods['name'] . "】审核通过", '');
                            break;
                        }
                    }
                }

                if ($shenhe == 'del') {
                    Db::name('user_address_goods')->where(['id' => ['in', $post]])->delete();
                } else {
                    $result = Db::name('user_address_goods')->where(['id' => ['in', $post]])->update(['status' => $shenhe, 'reason' => $reason, 'update_time' => $time]);
                }

                $data = ['ret' => '操作成功'];
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }

    /**
     * @return mixed
     */
    public function orders() {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        $this->assign('name', $search_name);
        $this->assign('search', $search_type);
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $where['t1.' . $search_type] = $search_name;
            } else {
                $where['t1.' . $search_type] = ['like', "%$search_name%"];
            }
        }

        $start = input('start');
        $end   = input('end');
        $this->assign('start', $start);
        $this->assign('end', $end);
        if ($start && $end) {
            $where['t1.update_time'] = ['between', [strtotime($start), strtotime($end)]];
        } elseif ($start && !$end) {
            $where['t1.update_time'] = ['egt', strtotime($start)];
        } elseif (!$start && $end) {
            $where['t1.update_time'] = ['elt', strtotime($end)];
        }

        $paystate = input('paystate');
        if ($paystate || $paystate === '0') {
            $this->assign('paystate', $paystate);
            $where['t1.paystate'] = $paystate;
        }
        $status = input('status');
        if ($status || $status === '0') {
            $this->assign('status', $status);
            $where['t1.status'] = $status;
        }
        $day  = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
        $data = Db::name('order_of_addressgood')
            ->alias('t1')
            ->join('user_claim_address t2', 't1.address_id = t2.id', 'left')
            ->join('user t3', 't1.user_id = t3.id', 'left')
            ->where($where)
            ->field('t1.*, t2.name addname, t2.image_3 addimage, t3.name username')
            ->order('t1.create_time desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function ($item) use ($day) {
                $item['data']     = json_decode($item['data'], true);
                $use_time         = $item['data']['use_time'];
                $item['use_time'] = '';
                if ($use_time) {
                    $use_time    = explode('|', $use_time);
                    $use_time[0] = explode(',', $use_time[0]);
                    $daylen      = sizeof($use_time[0]);
                    $use_times   = [];
                    if ($daylen > 1) {
                        $next        = 1;
                        $index       = 0;
                        $use_times[] = [$day[0]];
                        while ($next) {
                            if ($use_time[0][$next - 1] == $use_time[0][$next] - 1) {
                                $use_times[$index][1] = $day[$use_time[0][$next] - 1];
                                if (isset($use_time[0][$next + 1])) {
                                    $next++;
                                } else {
                                    $next = 0;
                                }
                            } else {
                                $index++;
                                $use_times[$index][0] = $day[$use_time[0][$next] - 1];
                                if (isset($use_time[0][$next + 1])) {
                                    $next++;
                                } else {
                                    $next = 0;
                                }
                            }
                        }
                        foreach ($use_times as $key => $value) {
                            $use_times[$key] = implode('至', $use_times[$key]);
                        }
                    } else {
                        foreach ($use_time[0] as $key => $value) {
                            $use_times[] = $day[$value - 1];
                        }
                    }
                    $item['use_time'] = implode(',', $use_times) . ' ' . $use_time[1];
                } else {
                    $item['use_time'] = '店铺营业时间';
                }
                return $item;
            });

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * @return mixed
     */
    public function orderinfo() {
        $data = Db::name('order_of_addressgood')
            ->where(['id' => input('id')])
            ->find();
        if (!$data) {
            exit('订单不存在');
        }
        $data['data']        = json_decode($data['data'], true);
        $data['store']       = Db::name('user_claim_address')->where(['id' => $data['address_id'], 'state' => 1])->cache(60)->find();
        $data['member']      = Db::name('user')->where(['id' => $data['user_id']])->cache(60)->find();
        $data['coupon']      = Db::name('user_coupon')->where(['id' => $data['coupon_id']])->cache(60)->find();
        $data['coupon']      = $data['coupon'] ? $data['coupon'] : false;
        $data['expire_time'] = date('Y-m-d H:i:s', $data['expire_time']);
        $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
        $data['update_time'] = date('Y-m-d H:i:s', $data['update_time']);
        $data['pay_time']    = date('Y-m-d H:i:s', $data['pay_time']);
        $this->assign('name', $data);
        $this->assign('name', $data);
        return $this->fetch();
    }

    public function setOrderStatus($id,$status)
    {
        $result = Db::name('order_of_addressgood')->where(['id'=>$id, 'paystate'=>1])->setField('status', $status);
        if ($result) {
            return ['ret'=>'操作成功'];
        }
        return ['err'=>'操作失败'];
    }
}